package cn.JHai.rbac.service.imp;


import cn.JHai.rbac.comm.CurrentUtil;

import cn.JHai.rbac.domain.Customertransfer;
import cn.JHai.rbac.domain.Employee;

import cn.JHai.rbac.mapper.CustomertransferMapper;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.CustomerTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CustomerTransferServiceImp implements CustomerTransferService {


    @Autowired
    private CustomertransferMapper customerTransferMapper;


    @Override
    public int saveOrUpdata(Customertransfer CustomerTransfer, Long customer_id) {
        //是插入
        if(CustomerTransfer.getId()==null){
            Employee current = CurrentUtil.getCurrent();

            CustomerTransfer.setCustomer_id(customer_id);
            CustomerTransfer.setOperator_id(current.getId());
            CustomerTransfer.setOperate_time(new Date());
            System.out.println(CustomerTransfer);
            return customerTransferMapper.insert(CustomerTransfer);
        }else {
            //跟新
            return customerTransferMapper.updateByPrimaryKey(CustomerTransfer);
        }
    }


    @Override
    public PageResult query(QueryObject queryObject) {

        //全局查询
        if(queryObject.getKeyword() == null || queryObject.getKeyword().equals("")){
            Long count = customerTransferMapper.selectForCount(queryObject);
            return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),customerTransferMapper.pageSelect(queryObject),count);
        }
        //关键字查询
        else{
            Long count = customerTransferMapper.selectForCountWithKeyword(queryObject);
            return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),customerTransferMapper.pageSelectWithKeyword(queryObject),count);
        }
    }

}
